var componentsViewArr:Object;
var cachedScene:String;

/*var historyViewArr:Array;
var historyListMax:Number=20;*/

var basicViewCount:Number=0;
var halfFlag:Boolean

function loadScene(sceneName:String, halfFlg:Boolean)
{
	if (Packages.system.display.SceneTransition.processFlag) return;
	halfFlag=halfFlg;
	Packages.system.mlf.DeepLinking.setDeepLinkingValue(sceneName);
}

function loadPath(value:String)
{
	if (componentsViewArr==null)
	{
		mlf.mcmanager.MessageAgent.getMessageDispatcher().dispatch({type:"onCoverProgress", param:"Load UI ..."});
		if (value!="content/Login")
		{
			cachedScene=value;
		}
		
		initComponentsViewArr();
		loadScene("content/Login?redirect");

		// load content by cachedScene
		return;
	}

	// add2HistoryList(value);

	if (value.indexOf("Login")==-1)
	{
		if (!Packages.model.Context.loginFlag) return;
	}

	if (halfFlag)
	{
		doLoadPath(value);
		halfFlag=false;
	}
	else if (Packages.model.Context.loginFlag) Packages.system.display.SceneTransition.rollIn(value);
	else doLoadPath(value);
}

function doLoadPath(value:String)
{
	// Packages.log("value: "+value, "debug");
	var qIndex=value.indexOf("?");
	if (qIndex>-1)
	{
		value=value.substr(0, qIndex);
	}

	// Packages.log("value: "+value, "debug");
	
	var path="app/"+value+".xml";
	var slashIndex=value.lastIndexOf("/");
	var slot:String;
	if (slashIndex<0)
	{
		slot=value;
	}
	else slot=value.substr(slashIndex+1);

	// Packages.log("path: "+path, "debug");
	
	Packages.system.mlf.GateKeeper.unloadML2bySlot(componentsViewArr.content);
	Packages.model.Context.activeContent["dispose"].apply(Packages.model.Context.activeContent, null);
	// Packages.log("removed content: "+componentsViewArr.content);
	
	Packages.components.basic.ThemeBg.refresh();

	componentsViewArr.content=slot;
	Packages.system.mlf.GateKeeper.loadML2byPath(path);

	// trace("Packages.system.mlf.GateKeeper.loadML2byPath: "+path);
}

/*function add2HistoryList(value:String)
{
	if (historyViewArr.length>=historyListMax)
	{
		historyViewArr.shift();
	}

	historyViewArr.push(value);
}*/

function initComponentsViewArr()
{
	/*if (value!="content/Login")
	{
		cachedScene=value;
	}
	
	Packages.system.mlf.GateKeeper.unloadML2bySlot("Libs");*/
	
	componentsViewArr=new Object();
	componentsViewArr.basic=new Array();
	componentsViewArr.basic.push("ThemeBg");
	Packages.system.mlf.GateKeeper.loadML2byPath("app/basic/ThemeBg.xml");
	componentsViewArr.basic.push("NavigationBar");
	Packages.system.mlf.GateKeeper.loadML2byPath("app/basic/NavigationBar.xml");
	
	// componentsViewArr.tools=new Array();
	// Packages.system.mlf.GateKeeper.loadML2byPath("app/Blank.xml");

	componentsViewArr.content="Libs";
	//componentsViewArr.content="Login";
	//Packages.system.mlf.GateKeeper.loadML2byPath("app/content/Login.xml");
}

function checkBasicViewCount()
{
	basicViewCount++;
	if (basicViewCount==2)
	{
		mlf.mcmanager.MessageAgent.getMessageDispatcher().dispatch({type:"onCoverProgress", param:"End"});

		Packages.system.display.SceneTransition.rollOut(true);
	}
}

function loadCacheScene()
{
	// Packages.system.mlf.DeepLinking.setDeepLinkingValue(cachedScene);
	// Packages.log("cacheScene: "+cachedScene, "debug");
	if (cachedScene!=null)
	{
		Packages.components.SceneAgent.loadScene(cachedScene);
		cachedScene=null;
	}
	else 
	{
		Packages.components.SceneAgent.loadScene("content/HomePage");
	}
	// Packages.system.display.SceneTransition.rollIn(cachedScene);
}